
 <!DOCTYPE HTML>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
  
    <title>flying-阳春 新增内容 | my bat is flying in sunny-spring</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
    
    <meta name="author" content="limeng32">
    

    
    <meta name="description" content="本节内容向您讲解阳春版本新增的在 flying 表达式中指定查询数据库和 customTypeHandler 特性">
<meta property="og:type" content="article">
<meta property="og:title" content="flying-阳春 新增内容">
<meta property="og:url" content="http://flying-doc.limeng32.com/2017/04/15/2017-04-15-flying-阳春 新增特性/index.html">
<meta property="og:site_name" content="my bat is flying in sunny-spring">
<meta property="og:description" content="本节内容向您讲解阳春版本新增的在 flying 表达式中指定查询数据库和 customTypeHandler 特性">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-04-09T08:28:10.305Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="flying-阳春 新增内容">
<meta name="twitter:description" content="本节内容向您讲解阳春版本新增的在 flying 表达式中指定查询数据库和 customTypeHandler 特性">

    
    <link rel="alternative" href="/atom.xml" title="my bat is flying in sunny-spring" type="application/atom+xml">
    
    
    
    <link rel="stylesheet" href="/css/style.css">
</head>

  <body>
    <header>
      
<div>
		
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="my bat is flying in sunny-spring">my bat is flying in sunny-spring</a></h1>
				<h2 class="blog-motto"></h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜單">
			</a></div>
			<nav class="animated">
				<ul>
					<ul>
					 
						<li><a href="/">Home</a></li>
					
						<li><a href="/archives">Archives</a></li>
					
						<li><a href="/about">About</a></li>
					
					<li>
 					
					<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
						<label>Search</label>
						<input type="search" id="search" name="q" autocomplete="off" maxlength="20" placeholder="搜索" />
						<input type="hidden" name="q" value="site:flying-doc.limeng32.com">
					</form>
					
					</li>
				</ul>
			</nav>			
</div>
    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
  
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2017/04/15/2017-04-15-flying-阳春 新增特性/" title="flying-阳春 新增内容" itemprop="url">flying-阳春 新增内容</a>
  </h1>
  <p class="article-author">By
       
		<a href="/about" title="limeng32" target="_blank" itemprop="author">limeng32</a>
		
  <p class="article-time">
    <time datetime="2017-04-14T16:00:00.000Z" itemprop="datePublished"> 發表於 2017-04-15</time>
    
  </p>
</header>
	<div class="article-content">
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目錄</strong>
		
		<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#flying-阳春"><span class="toc-text"><a href="#flying-&#x9633;&#x6625;" class="headerlink" title="flying-&#x9633;&#x6625;"></a><a href="#flying-&#x9633;&#x6625;">flying-&#x9633;&#x6625;</a></span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#高性能的跨数据库查询"><span class="toc-text"><a href="#&#x9AD8;&#x6027;&#x80FD;&#x7684;&#x8DE8;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;" class="headerlink" title="&#x9AD8;&#x6027;&#x80FD;&#x7684;&#x8DE8;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;"></a><a href="#&#x9AD8;&#x6027;&#x80FD;&#x7684;&#x8DE8;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;">&#x9AD8;&#x6027;&#x80FD;&#x7684;&#x8DE8;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;</a></span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#customTypeHandler"><span class="toc-text"><a href="#customTypeHandler" class="headerlink" title="customTypeHandler"></a><a href="#customTypeHandler">customTypeHandler</a></span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#跨库或逻辑查询"><span class="toc-text"><a href="#&#x8DE8;&#x5E93;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;" class="headerlink" title="&#x8DE8;&#x5E93;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;"></a><a href="#&#x8DE8;&#x5E93;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;">&#x8DE8;&#x5E93;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;</a></span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#flying-初雪"><span class="toc-text"><a href="#flying-&#x521D;&#x96EA;" class="headerlink" title="flying-&#x521D;&#x96EA;"></a><a href="#flying-&#x521D;&#x96EA;">flying-&#x521D;&#x96EA;</a></span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#自定义主键生成器"><span class="toc-text"><a href="#&#x81EA;&#x5B9A;&#x4E49;&#x4E3B;&#x952E;&#x751F;&#x6210;&#x5668;" class="headerlink" title="&#x81EA;&#x5B9A;&#x4E49;&#x4E3B;&#x952E;&#x751F;&#x6210;&#x5668;"></a><a href="#&#x81EA;&#x5B9A;&#x4E49;&#x4E3B;&#x952E;&#x751F;&#x6210;&#x5668;">&#x81EA;&#x5B9A;&#x4E49;&#x4E3B;&#x952E;&#x751F;&#x6210;&#x5668;</a></span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用或逻辑查询"><span class="toc-text"><a href="#&#x4F7F;&#x7528;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;" class="headerlink" title="&#x4F7F;&#x7528;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;"></a><a href="#&#x4F7F;&#x7528;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;">&#x4F7F;&#x7528;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;</a></span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#普通或逻辑查询"><span class="toc-text"><a href="#&#x666E;&#x901A;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;" class="headerlink" title="&#x666E;&#x901A;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;"></a><a href="#&#x666E;&#x901A;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;">&#x666E;&#x901A;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;</a></span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#外键或逻辑查询"><span class="toc-text"><a href="#&#x5916;&#x952E;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;" class="headerlink" title="&#x5916;&#x952E;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;"></a><a href="#&#x5916;&#x952E;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;">&#x5916;&#x952E;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;</a></span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#优化使用体验"><span class="toc-text"><a href="#&#x4F18;&#x5316;&#x4F7F;&#x7528;&#x4F53;&#x9A8C;" class="headerlink" title="&#x4F18;&#x5316;&#x4F7F;&#x7528;&#x4F53;&#x9A8C;"></a><a href="#&#x4F18;&#x5316;&#x4F7F;&#x7528;&#x4F53;&#x9A8C;">&#x4F18;&#x5316;&#x4F7F;&#x7528;&#x4F53;&#x9A8C;</a></span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#全面提升代码质量"><span class="toc-text"><a href="#&#x5168;&#x9762;&#x63D0;&#x5347;&#x4EE3;&#x7801;&#x8D28;&#x91CF;" class="headerlink" title="&#x5168;&#x9762;&#x63D0;&#x5347;&#x4EE3;&#x7801;&#x8D28;&#x91CF;"></a><a href="#&#x5168;&#x9762;&#x63D0;&#x5347;&#x4EE3;&#x7801;&#x8D28;&#x91CF;">&#x5168;&#x9762;&#x63D0;&#x5347;&#x4EE3;&#x7801;&#x8D28;&#x91CF;</a></span></a></li></ol></li></ol>
		
		</div>
		
		<h1 id="flying-阳春"><a href="#flying-阳春" class="headerlink" title="flying-阳春"></a><a href="#flying-阳春">flying-阳春</a></h1><p><code>阳春</code> 对应 flying 版本号为 <code>0.9.4</code>（适用于 mybatis-3.4.x），主要新增特性如下：</p>
<h2 id="高性能的跨数据库查询"><a href="#高性能的跨数据库查询" class="headerlink" title="高性能的跨数据库查询"></a><a href="#高性能的跨数据库查询">高性能的跨数据库查询</a></h2><p>经过两个版本的沉淀，现在 flying 终于推出了简单易用的跨库查询语法，具体如下：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">flying#&#123;?&#125;(smartDataSourceId:dataBaseName):select</span><br></pre></td></tr></table></figure></p>
<p>这个方法永远都会使用 <code>dataBaseName</code> 数据库，无论它在哪个数据源内被调用；如果需要跨源，它会使用 <code>smartDataSourceId</code> 数据源（请确保这是一个智能数据源）；</p>
<p>同时从此版本开始，您需要将需要将<a href="https://github.com/limeng32/mybatis.flying/blob/master/src/main/java/indi/mybatis/flying/FlyingContextProvider.java" target="_blank" rel="noopener"> flying 的语境访问器</a> 引入您的 spring 环境配置中，具体方式有两种，一种是<a href="https://github.com/limeng32/mybatis.flying/blob/master/src/test/resources/spring-test.xml" target="_blank" rel="noopener">在环境 spring 配置文件中加入bean</a>，另一种是在 spring 配置文件的扫描器中加上 “indi.mybatis.flying” 路径，如下：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">context:component-scan</span> <span class="attr">base-package</span>=<span class="string">"indi.demo.flying.*, indi.mybatis.flying"</span> /&gt;</span></span><br></pre></td></tr></table></figure></p>
<h2 id="customTypeHandler"><a href="#customTypeHandler" class="headerlink" title="customTypeHandler"></a><a href="#customTypeHandler">customTypeHandler</a></h2><p>在 <code>flying-阳春</code> 中 <code>@FieldMapperAnnotation</code> 和 <code>@ConditionMapperAnnotation</code> 增加了 <code>customTypeHandler</code> 属性，使您可以用自定义的 TypeHandler 来处理变量映射，因为  <code>customTypeHandler</code> 具有最高优先级。一个使用场景如下：</p>
<h3 id="跨库或逻辑查询"><a href="#跨库或逻辑查询" class="headerlink" title="跨库或逻辑查询"></a><a href="#跨库或逻辑查询">跨库或逻辑查询</a></h3><p><code>customTypeHandler</code> 使跨库或查询成为可能，但仅限于外键 ID 相等这一种条件。我们假设 person 和 role 不在一个数据库中，如果想查询对应role 的 ID 为 1 或者为 2 的 person，我们可以在 person 的条件类中加入如下变量：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> org.apache.ibatis.type.IntegerTypeHandler;</span><br><span class="line"></span><br><span class="line"><span class="meta">@Or</span>(&#123; </span><br><span class="line">  <span class="meta">@ConditionMapperAnnotation</span>(dbFieldName = <span class="string">"role_id"</span>, conditionType = ConditionType.Equal, customTypeHandler = IntegerTypeHandler.class),</span><br><span class="line">  <span class="meta">@ConditionMapperAnnotation</span>(dbFieldName = <span class="string">"role_id"</span>, conditionType = ConditionType.Equal, customTypeHandler = IntegerTypeHandler.class) &#125;)</span><br><span class="line"><span class="keyword">private</span> Object[] role1EqualsOrRole2Equals;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> Object[] getRole1EqualsOrRole2Equals () &#123;</span><br><span class="line">	<span class="keyword">return</span> role1EqualsOrRole2Equals;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setRole1EqualsOrRole2Equals</span> <span class="params">(Object... role1EqualsOrRole2Equals)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">this</span>. role1EqualsOrRole2Equals = role1EqualsOrRole2Equals;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>如果想查询对应role 的 ID 为 1 或者为 2 的 person 的话只需如下代码即可：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">PersonCondition pc = <span class="keyword">new</span> PersonCondition();</span><br><span class="line">pc.setRole1EqualsOrRole2Equals(<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line">Person&lt;Collection&gt; persons = personService.selectAll(pc);</span><br></pre></td></tr></table></figure>
<h1 id="flying-初雪"><a href="#flying-初雪" class="headerlink" title="flying-初雪"></a><a href="#flying-初雪">flying-初雪</a></h1><p><code>初雪</code> 对应 flying 版本号为 <code>0.8.3</code>（适用于 mybatis-3.3.x）和 <code>0.9.3</code>（适用于 mybatis-3.4.x），主要新增特性如下：</p>
<h2 id="自定义主键生成器"><a href="#自定义主键生成器" class="headerlink" title="自定义主键生成器"></a><a href="#自定义主键生成器">自定义主键生成器</a></h2><p>为满足个性化主键需要，flying 新增了自定义主键生成器，为此我们在flying:insert语句中新增了括号元素，如下：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">flying:insert(uuid)                      使用标准uuid作主键</span><br><span class="line">flying:insert(uuid_no_line)              使用无下横线的uuid作主键</span><br><span class="line">flying:insert(millisecond)               使用毫秒数作主键（需保证每秒并发在1000以下）</span><br></pre></td></tr></table></figure></p>
<p>当然更多的情况是您会自定义自己的主键生成器，只要您的主键生成器实现了 flying 中的 indi.mybatis.flying.type.KeyHandler 接口即可，比如这样调用一个自定义的主键生成器类：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">flying:insert(indi.mybatis.flying.handlers.MySnowFlakeKeyHandler)</span><br></pre></td></tr></table></figure></p>
<p>（上面的 indi.mybatis.flying.handlers.MySnowFlakeKeyHandler 是一个雪花主键生成器的 java 版本实现。雪花主键生成器由 tweeter 发明用于处理大规模并行写入，主键采用 float 类型存储以节省资源，自带递增无需 order by，单台主机每秒可产生 400 万个不同主键，最多可 1024 台主机集群同时工作。flying 中提供了一个实现方式，<a href="https://gitee.com/limeng32/mybatis.flying/blob/master/src/main/java/indi/mybatis/flying/handlers/SnowFlakeKeyHandler.java" target="_blank" rel="noopener">代码见此</a>）</p>
<p>在某些特殊业务场景中，您需要使用主键表达一定的业务含义，这时自定义主键生成器会显得非常有效。</p>
<p>这里有一个<a href="https://gitee.com/limeng32/flying-demo2/tree/use-flying-0.9.3/" target="_blank" rel="noopener">使用了自定义主键生成器的例子</a>，相信您看完以后会对此特性了然于胸。</p>
<h2 id="使用或逻辑查询"><a href="#使用或逻辑查询" class="headerlink" title="使用或逻辑查询"></a><a href="#使用或逻辑查询">使用或逻辑查询</a></h2><h3 id="普通或逻辑查询"><a href="#普通或逻辑查询" class="headerlink" title="普通或逻辑查询"></a><a href="#普通或逻辑查询">普通或逻辑查询</a></h3><p>“或”逻辑查询是 <code>初雪</code> 版本新增的最重要内容。为了实现此特性 flying 新增了 Or 标签类（<a href="https://gitee.com/limeng32/mybatis.flying/blob/master/src/main/java/indi/mybatis/flying/annotations/Or.java" target="_blank" rel="noopener">代码见此</a>），这个标签的内容是ConditionMapperAnnotation标签的数组，所以在查询条件类中可以有如下标签代码：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Or</span>(&#123;</span><br><span class="line">  <span class="meta">@ConditionMapperAnnotation</span>(dbFieldName = <span class="string">"name"</span>, conditionType = ConditionType.HeadLike),</span><br><span class="line">  <span class="meta">@ConditionMapperAnnotation</span>(dbFieldName = <span class="string">"age"</span>, conditionType = ConditionType.Equal), </span><br><span class="line">  <span class="meta">@ConditionMapperAnnotation</span>(dbFieldName = <span class="string">"name"</span>, conditionType = ConditionType.HeadLike) </span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure></p>
<p>（上面是实现 name like ‘XXX%’ or age = ‘YYY’ or name like ‘ZZZ%’ 查询的条件）</p>
<p>同时为了赋值方便，我们采用Object数组的不定参数形式作为变量类型，于是整个代码变成了：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Or</span>(&#123;</span><br><span class="line">  <span class="meta">@ConditionMapperAnnotation</span>(dbFieldName = <span class="string">"name"</span>, conditionType = ConditionType.HeadLike),</span><br><span class="line">  <span class="meta">@ConditionMapperAnnotation</span>(dbFieldName = <span class="string">"age"</span>, conditionType = ConditionType.Equal), </span><br><span class="line">  <span class="meta">@ConditionMapperAnnotation</span>(dbFieldName = <span class="string">"name"</span>, conditionType = ConditionType.HeadLike) </span><br><span class="line">&#125;)</span><br><span class="line"><span class="keyword">private</span> Object[] condition1;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> Object[] getCondition1 () &#123;</span><br><span class="line">	<span class="keyword">return</span> condition1;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCondition1</span> <span class="params">(Object... condition1)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">this</span>. condition1 = condition1;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>如果我们描述 “name like ‘张%’ or age = 27 or name like ‘李%’ “，代码如下：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">personCondition.setCondition1(<span class="string">"张"</span>, <span class="number">27</span>, <span class="string">"李"</span>);</span><br><span class="line"><span class="comment">/* 注意参数顺序和 condition1 上 @ConditionMapperOrAnnotation 的内部顺序一致 */</span></span><br></pre></td></tr></table></figure></p>
<p>您之前掌握的<a href="https://gitee.com/limeng32/mybatis.flying/blob/master/src/main/java/indi/mybatis/flying/statics/ConditionType.java" target="_blank" rel="noopener">绝大部分 ConditionMapperAnnotation</a> 都可以写在 <code>@Or</code> 中，只有本身就是集合型的条件类型例外，以下列出不能进入 <code>@Or</code> 的类型：</p>
<p><code>MultiLikeAND</code>、<code>MultiLikeOR</code>、 <code>In</code>、<code>NotIn</code></p>
<p>除此之外的查询条件均可以参与或查询。</p>
<h3 id="外键或逻辑查询"><a href="#外键或逻辑查询" class="headerlink" title="外键或逻辑查询"></a><a href="#外键或逻辑查询">外键或逻辑查询</a></h3><p>flying 在同库跨表查询时也可以做不同表上条件的或逻辑查询，比如我们要实现 person.name = ‘XXX’ or role.name = ‘YYY’ 查询，其中 role 是 person 业务上的父对象。我们可以在 role 的条件类中加入如下变量：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Or</span>(&#123; </span><br><span class="line">  <span class="meta">@ConditionMapperAnnotation</span>(dbFieldName = <span class="string">"name"</span>, conditionType = ConditionType.Equal),</span><br><span class="line">  <span class="meta">@ConditionMapperAnnotation</span>(dbFieldName = <span class="string">"name"</span>, conditionType = ConditionType.Equal, subTarget = mypackage.Person.class) &#125;)</span><br><span class="line"><span class="keyword">private</span> Object[] roleNameEqualsOrPersonNameEquals;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> Object[] getRoleNameEqualsOrPersonNameEquals () &#123;</span><br><span class="line">	<span class="keyword">return</span> roleNameEqualsOrPersonNameEquals;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setRoleNameEqualsOrPersonNameEquals</span> <span class="params">(Object... roleNameEqualsOrPersonNameEquals)</span> </span>&#123;</span><br><span class="line">	<span class="keyword">this</span>. roleNameEqualsOrPersonNameEquals = roleNameEqualsOrPersonNameEquals;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>上面的代码中第一行 <code>ConditionMapperAnnotation</code> 指的是 role 表，第二行指的是 person 表，因为是由 role 指向 person，所以第二行出现了 <code>subTarget</code> 参数用来引导路径，它的值就是业务上子对象的类路径。</p>
<p>值得注意的是，外键或逻辑查询中，跨表的 <code>@Or</code> 条件永远要写在业务上的父对象里，这是考虑到从子对象上寻找父对象并非唯一（例如多重外键情况，一个 person 有多个 role 型父对象，分别表示主要角色和次要角色等），然而从父对象上寻找子对象永远是唯一的。</p>
<p>如果您要查询用户名为“张三”或角色名为“wfadmin”的用户时，您只需这样做：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">RoleConditon rc = <span class="keyword">new</span> RoleCondition();</span><br><span class="line">rc.setRoleNameEqualsOrPersonNameEquals(<span class="string">"wfadmin"</span>,<span class="string">"张三"</span>);</span><br><span class="line">Person p = <span class="keyword">new</span> Person();</span><br><span class="line">p.setRole(rc);</span><br><span class="line">Person&lt;Collection&gt; persons = personService.selectAll(p);</span><br></pre></td></tr></table></figure></p>
<p>无论 role 是 person 业务上的直接父对象还是间接父对象都可以这样查询。</p>
<h2 id="优化使用体验"><a href="#优化使用体验" class="headerlink" title="优化使用体验"></a><a href="#优化使用体验">优化使用体验</a></h2><p><code>@QueryMapperAnnotation</code> 现在可以省略，只要您的某个类既继承实体 pojo 又实现 Conditionable 接口 flying 就可以判断出它是相关 pojo 的条件类。当然这个标签出现在代码里也没有任何问题。</p>
<h2 id="全面提升代码质量"><a href="#全面提升代码质量" class="headerlink" title="全面提升代码质量"></a><a href="#全面提升代码质量">全面提升代码质量</a></h2><p>在 <code>初雪</code> 中，我们把代码质量作为提升的重要一环，同时使用 阿里 P3C 和 SonarQube 来修正代码缺陷，当您阅读 flying 源码时您会深切感受到这一点。</p>
  
	</div>
		<footer class="article-footer clearfix">
<div class="article-catetags">

<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/blog/">blog</a>
</div>


</div>



	<div class="article-share" id="share">
	
	  <div data-url="http://flying-doc.limeng32.com/2017/04/15/2017-04-15-flying-阳春 新增特性/" data-title="flying-阳春 新增内容 | my bat is flying in sunny-spring" data-tsina="" class="share clearfix">
	  </div>
	
	</div>


</footer>

   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2017/05/01/2017-05-01-使用flying解决pojo自动映射问题/" title="使用 flying 解决 pojo 自动映射问题">
  <strong>上一篇：</strong><br/>
  <span>
  使用 flying 解决 pojo 自动映射问题</span>
</a>
</div>


<div class="next">
<a href="/2017/04/01/2017-04-01-使用flying完善Mybatis自带的二级缓存/"  title="使用 flying 完善Mybatis 自带的二级缓存">
 <strong>下一篇：</strong><br/> 
 <span>使用 flying 完善Mybatis 自带的二级缓存
</span>
</a>
</div>

</nav>

	



</div>  
      <div class="openaside"><a class="navbutton" href="#" title="顯示側邊欄"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">文章目錄</strong>
 
 <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#flying-阳春"><span class="toc-text"><a href="#flying-&#x9633;&#x6625;" class="headerlink" title="flying-&#x9633;&#x6625;"></a><a href="#flying-&#x9633;&#x6625;">flying-&#x9633;&#x6625;</a></span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#高性能的跨数据库查询"><span class="toc-text"><a href="#&#x9AD8;&#x6027;&#x80FD;&#x7684;&#x8DE8;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;" class="headerlink" title="&#x9AD8;&#x6027;&#x80FD;&#x7684;&#x8DE8;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;"></a><a href="#&#x9AD8;&#x6027;&#x80FD;&#x7684;&#x8DE8;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;">&#x9AD8;&#x6027;&#x80FD;&#x7684;&#x8DE8;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;</a></span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#customTypeHandler"><span class="toc-text"><a href="#customTypeHandler" class="headerlink" title="customTypeHandler"></a><a href="#customTypeHandler">customTypeHandler</a></span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#跨库或逻辑查询"><span class="toc-text"><a href="#&#x8DE8;&#x5E93;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;" class="headerlink" title="&#x8DE8;&#x5E93;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;"></a><a href="#&#x8DE8;&#x5E93;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;">&#x8DE8;&#x5E93;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;</a></span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#flying-初雪"><span class="toc-text"><a href="#flying-&#x521D;&#x96EA;" class="headerlink" title="flying-&#x521D;&#x96EA;"></a><a href="#flying-&#x521D;&#x96EA;">flying-&#x521D;&#x96EA;</a></span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#自定义主键生成器"><span class="toc-text"><a href="#&#x81EA;&#x5B9A;&#x4E49;&#x4E3B;&#x952E;&#x751F;&#x6210;&#x5668;" class="headerlink" title="&#x81EA;&#x5B9A;&#x4E49;&#x4E3B;&#x952E;&#x751F;&#x6210;&#x5668;"></a><a href="#&#x81EA;&#x5B9A;&#x4E49;&#x4E3B;&#x952E;&#x751F;&#x6210;&#x5668;">&#x81EA;&#x5B9A;&#x4E49;&#x4E3B;&#x952E;&#x751F;&#x6210;&#x5668;</a></span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用或逻辑查询"><span class="toc-text"><a href="#&#x4F7F;&#x7528;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;" class="headerlink" title="&#x4F7F;&#x7528;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;"></a><a href="#&#x4F7F;&#x7528;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;">&#x4F7F;&#x7528;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;</a></span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#普通或逻辑查询"><span class="toc-text"><a href="#&#x666E;&#x901A;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;" class="headerlink" title="&#x666E;&#x901A;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;"></a><a href="#&#x666E;&#x901A;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;">&#x666E;&#x901A;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;</a></span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#外键或逻辑查询"><span class="toc-text"><a href="#&#x5916;&#x952E;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;" class="headerlink" title="&#x5916;&#x952E;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;"></a><a href="#&#x5916;&#x952E;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;">&#x5916;&#x952E;&#x6216;&#x903B;&#x8F91;&#x67E5;&#x8BE2;</a></span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#优化使用体验"><span class="toc-text"><a href="#&#x4F18;&#x5316;&#x4F7F;&#x7528;&#x4F53;&#x9A8C;" class="headerlink" title="&#x4F18;&#x5316;&#x4F7F;&#x7528;&#x4F53;&#x9A8C;"></a><a href="#&#x4F18;&#x5316;&#x4F7F;&#x7528;&#x4F53;&#x9A8C;">&#x4F18;&#x5316;&#x4F7F;&#x7528;&#x4F53;&#x9A8C;</a></span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#全面提升代码质量"><span class="toc-text"><a href="#&#x5168;&#x9762;&#x63D0;&#x5347;&#x4EE3;&#x7801;&#x8D28;&#x91CF;" class="headerlink" title="&#x5168;&#x9762;&#x63D0;&#x5347;&#x4EE3;&#x7801;&#x8D28;&#x91CF;"></a><a href="#&#x5168;&#x9762;&#x63D0;&#x5347;&#x4EE3;&#x7801;&#x8D28;&#x91CF;">&#x5168;&#x9762;&#x63D0;&#x5347;&#x4EE3;&#x7801;&#x8D28;&#x91CF;</a></span></a></li></ol></li></ol>
 
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隱藏側邊欄"></a></div>
<aside class="clearfix">

  <div class="linkslist">
  <p class="asidetitle">友情鏈接</p>
    <ul>
        
          <li>
            
            	<a href="https://my.oschina.net/u/2280950/blog/1580056" target="_blank" title="关于在网络应用中使用双向相关模型的研究">flying-初雪 理论基础一</a>
            
          </li>
        
          <li>
            
            	<a href="https://my.oschina.net/u/2280950/blog/1594694" target="_blank" title="或逻辑的需求的实现方案">flying-初雪 理论基础二</a>
            
          </li>
        
          <li>
            
            	<a href="http://www.cnblogs.com/dasn/articles/5208022.html" target="_blank" title="为什么我们在初雪版中引入自定义主键机制">flying-初雪 新增自定义主键生成器特性</a>
            
          </li>
        
    </ul>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> God is in his heaven, all&#39;s right with the code. </p>
	</section>
	 
	<div class="social-font" class="clearfix">
		
		
		<a href="https://gitee.com/limeng32/mybatis.flying" target="_blank" class="icon-gitee" title="gitee"></a>
		
		
		<a href="https://github.com/limeng32/mybatis.flying" target="_blank" class="icon-github" title="github"></a>
		
		
		
		
		
		
		
		<a href="http://www.zhihu.com/people/li-meng-48" target="_blank" class="icon-zhihu" title="知乎"></a>
		
		
		
	</div>
			
		

	<p class="copyright">
		written by <a href="/about" target="_blank" title="limeng32">limeng32</a>
		© 2018
	</p>
</div>
</footer>
    <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/jquery.imagesloaded.min.js"></script>
<script src="/js/gallery.js"></script>
<script src="/js/jquery.qrcode-0.12.0.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
  
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else{
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  c.click(function(){
    ta.css('display', 'block').addClass('fadeIn');
  });
  o.click(function(){
    ta.css('display', 'none');
  });
  $(window).scroll(function(){
    ta.css("top",Math.max(140,320-$(this).scrollTop()));
  });
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina'),
      description = $this.attr('description');
  var html = [
  '<div class="hoverqrcode clearfix"></div>',
  '<a class="overlay" id="qrcode"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="微信"></a>',
  '<a href="http://widget.renren.com/dialog/share?resourceUrl=' + encodedUrl + '&srcUrl=' + encodedUrl + '&title=' + title +'" class="article-share-renren" target="_blank" title="人人"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="微博"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);

  $('.hoverqrcode').hide();

  var myWidth = 0;
  function updatehoverqrcode(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
    var qrsize = myWidth > 1024 ? 200:100;
    var options = {render: 'image', size: qrsize, fill: '#2ca6cb', text: url, radius: 0.5, quiet: 1};
    var p = $('.article-share-qrcode').position();
    $('.hoverqrcode').empty().css('width', qrsize).css('height', qrsize)
                          .css('left', p.left-qrsize/2+20).css('top', p.top-qrsize-10)
                          .qrcode(options);
  };
  $(window).resize(function(){
    $('.hoverqrcode').hide();
  });
  $('.article-share-qrcode').click(function(){
    updatehoverqrcode();
    $('.hoverqrcode').toggle();
  });
  $('.article-share-qrcode').hover(function(){}, function(){
      $('.hoverqrcode').hide();
  });
});   
</script>











<link rel="stylesheet" href="/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



<!-- Analytics Begin -->





<!-- Analytics End -->

<!-- Totop Begin -->

	<div id="totop">
	<a title="返回頂部"><img src="/img/scrollup.png"/></a>
	</div>
	<script src="/js/totop.js"></script>

<!-- Totop End -->

<!-- MathJax Begin -->
<!-- mathjax config similar to math.stackexchange -->


<!-- MathJax End -->

<!-- Tiny_search Begin -->

<!-- Tiny_search End -->

  </body>
</html>
